﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace block_explorer
{
    class DebugTextStacker : DrawableGameComponent
    {
        public enum GrowDirection { UP, DOWN }
        public enum HorizAnchor { LEFT, RIGHT }

        private SpriteBatch mSpriteBatch;
        private Vector2 mPosition;
        private SpriteFont mFont;
        private Color mColor;
        private List<string> mLines;
        private GrowDirection mDirection;
        private HorizAnchor mHorizAnchor;

        public Vector2 Position { get { return mPosition; } set { mPosition = value; } }
        public SpriteFont Font { get { return mFont; } set { mFont = value; } }
        public Color Color { get { return mColor; } set { mColor = value; } }
        public GrowDirection Direction { get { return mDirection; } set { mDirection = value; } }
        public HorizAnchor HorizontalAnchor { get { return mHorizAnchor; } set { mHorizAnchor = value; } }

        public DebugTextStacker(BlockExplorer game)
            : base(game)
        {
            mLines = new List<string>();
            mDirection = GrowDirection.DOWN;
            mHorizAnchor = HorizAnchor.LEFT;
        }

        public void AddLine(string line)
        {
            mLines.Add(line);
        }

        public void Clear()
        {
            mLines.Clear();
        }

        public override void Initialize()
        {
            base.Initialize();

            mSpriteBatch = new SpriteBatch(Game.GraphicsDevice);
        }

        public override void Draw(GameTime gameTime)
        {
            mSpriteBatch.Begin();

            Vector2 pos = mPosition;
            foreach (string line in mLines)
            {
                Vector2 lineDimensions = mFont.MeasureString(line);
                if (mDirection == GrowDirection.UP) pos.Y -= lineDimensions.Y;
                if (mHorizAnchor == HorizAnchor.RIGHT) pos.X = mPosition.X - lineDimensions.X;
                mSpriteBatch.DrawString(mFont, line, pos, mColor);
                if (mDirection == GrowDirection.DOWN) pos.Y += lineDimensions.Y;
            }

            mSpriteBatch.End();
        }
    }
}
